/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller.filemanager;

/**
 *
 * @author Wahyu Fahmy
 */
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class ReadFile {
    
    /*** properties ***/
    private File file = null;
    private FileInputStream fis = null;
    private BufferedInputStream bis = null;
    private BufferedReader br = null;
    
    private String filename = "";
    private ArrayList<String> content = new ArrayList<String>();

    /*** methods ***/
    public ReadFile(String filename){
        try {
            this.filename = filename;
            this.file = new File(filename);
            this.open();
            this.content = this.fetchContent();
        } catch (FileNotFoundException e) {
            //@todo
        } catch (IOException e) {
            //@todo
        }
    }

    private ArrayList<String> fetchContent() throws IOException {
        ArrayList<String> isi = new ArrayList<String>();
        int i = 0;
        String line = null;
        while (((line = br.readLine()) != null) /*&& ((line = br.readLine()) != "")*/){
            isi.add(line);
            i++;
        }
        return isi;
    }
    
    /*** interface for another class ***/

    public void open() throws FileNotFoundException {
        fis = new FileInputStream(file);

        // Here BufferedInputStream is added for fast reading.
        bis = new BufferedInputStream(fis);
        br = new BufferedReader(new InputStreamReader(bis));
    }

    public void close(){
        try {
            fis.close();
            bis.close();
            br.close();
        } catch (IOException ex) {
            Logger.getLogger(ReadFile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getFileName(){
        return this.filename;
    }

    public ArrayList<String> getContent(){
        return this.content;
    }
    
    /*** for testing only ***/

    private void printContent(){
        ListIterator<String> litr = this.content.listIterator();
        while (litr.hasNext()) {
            String element = litr.next();
            System.out.println(element);
        }
    }

    public static void main(String[] args) {
        ReadFile rf = new ReadFile(
                "data/by 192.168.10.111.txt"
            );
        rf.printContent();
        rf.close();
    }
}
